import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { Body, _HttpClient } from '@delon/theme';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ApiService } from '@core/services/api';
import { STData } from '@delon/abc';

@Component({
  selector: 'upgrade-edit',
  templateUrl: './upgrade-edit.component.html',
  styles: ['>>>.w200{width:200px;}']
})
export class UpgradeEditComponent implements OnInit {
  @ViewChild('st', { static: false }) st: TablePageComponent;
  selectData: any[];
  loading = false;
  addUserInfo = {
    userAccount: '',
    userPassword: '',
    userName: '',
    roles: [],
    depts: []
  };
  @Input()
  set _param(value) {
    if (!value) return;
    this.param = value;
  };
  param: any;
  config: SPage = {
    columns: [
      {
        index: 'createTime',
        title: '创建时间',
        className:'w200'
      },
      {
        index:'recordResult',
        title:'升级结果'
      }],
    showCheckBox: false,
    data:[]
  }
  constructor(private http: _HttpClient,  private api: ApiService, private modal: NzModalService, private message: NzMessageService) { }
  //this.param.id
  ngOnInit() {
    this.http.post('/core/upgrade/record/page', {deviceId:this.param.id,order:'createTime desc'}).subscribe((res:any) => {
      if (res.status == '200') {
        this.config.data = res.data.list;
      }
    })
  }


  _console(e: any): void {
    this.selectData = e;
  }
  //升级设备
  updateAll() {
    if (!!this.selectData && this.selectData.length !== 0) {
      this.modal.confirm({
        nzTitle: '您确定要升级这些设备吗',
        nzOnOk: () => {
          const body = [];
          this.selectData.forEach(item => {
            body.push({id:item.id,status:false});
          })
          this.http.put('/core/host/batch',body).subscribe(res => {
            if(res.status === '200'){
              this.message.success('升级成功');
              this.st.reload();
            }
          })
        }
      })
    } else {
      this.message.warning('请选择设备');
    }
  }
  

}
